﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Dack_Web2.DTO;
using System.Data.OleDb;

namespace Dack_Web2.DAO
{
    public class KhachHangDAO
    {
        public void Insert(KhachHangDTO tmp)
        {
            OleDbConnection cn = Sql_DataProvider.SQLConnect();
            string str = "insert into KhachHang(TenDangNhap, TenKhachHang, NgaySinh, DiaChi, SoDienThoai, GioiTinh) values (@TenDangNhap, @TenKhachHang, @NgaySinh, @DiaChi, @SoDienThoai, @GioiTinh)";
            OleDbCommand cmd = new OleDbCommand(str, cn);

            OleDbParameter para1 = new OleDbParameter("@TenDangNhap", tmp.TenDangNhap);
            OleDbParameter para2 = new OleDbParameter("@TenKhachHang", tmp.TenKhachHang);
            OleDbParameter para3 = new OleDbParameter("@NgaySinh", tmp.NgaySinh);
            OleDbParameter para4 = new OleDbParameter("@DiaChi", tmp.DiaChi);
            OleDbParameter para5 = new OleDbParameter("@SoDienThoai", tmp.SoDienThoai);
            OleDbParameter para6 = new OleDbParameter("@GioiTinh", tmp.GioiTinh);

            cmd.Parameters.Add(para1);
            cmd.Parameters.Add(para2);
            cmd.Parameters.Add(para3);
            cmd.Parameters.Add(para4);
            cmd.Parameters.Add(para5);
            cmd.Parameters.Add(para6);

            cmd.ExecuteNonQuery();

            cn.Close();
        }

        public void Update(KhachHangDTO tmp)
        {
            
            string str = "update [KhachHang] set TenKhachHang=@TenKhachHang, NgaySinh=@NgaySinh, DiaChi=@DiaChi, SoDienThoai=@SoDienThoai, GioiTinh=@GioiTinh where TenDangNhap=@TenDangNhap";
            
            OleDbParameter[] para1 = {new OleDbParameter("@TenKhachHang", tmp.TenKhachHang),
                                        new OleDbParameter("@NgaySinh", tmp.NgaySinh),
                                        new OleDbParameter("@DiaChi", tmp.DiaChi),
                                        new OleDbParameter("@SoDienThoai", tmp.SoDienThoai),
                                        new OleDbParameter("@GioiTinh", tmp.GioiTinh),
                                        new OleDbParameter("@TenDangNhap", tmp.TenDangNhap)};

            Sql_DataProvider.SQL_ExecuteNonQuery(str, para1);

            
        }

        public void Delete(KhachHangDTO tmp)
        {
            OleDbConnection cn = Sql_DataProvider.SQLConnect();
            string str = "delete from KhachHang where TenDangNhap=@TenDangNhap";
            OleDbCommand cmd = new OleDbCommand(str, cn);

            OleDbParameter para1 = new OleDbParameter("@TenDangNhap", tmp.TenDangNhap);

            cmd.Parameters.Add(para1);

            cmd.ExecuteNonQuery();

            cn.Close();
        }
        public static KhachHangDTO  GetKhachHang(string TenDangNhap)
        {
            KhachHangDTO KH = new KhachHangDTO();
            string sql = string.Format("select * from [KhachHang] where TenDangNhap = '{0}'", TenDangNhap);
            OleDbDataReader Read = Sql_DataProvider.SQL_ExcuteQueryReader(sql);
            if (Read.Read())
            {                
                KH.DiaChi = Read["DiaChi"].ToString();
                KH.GioiTinh = (bool)Read["GioiTinh"] == true ? true: false;
                KH.NgaySinh = Convert.ToDateTime(Read["NgaySinh"]);
                KH.SoDienThoai = Read["SoDienThoai"].ToString();
                KH.TenDangNhap = Read["TenDangNhap"].ToString();
                KH.TenKhachHang = Read["TenKhachHang"].ToString();
                return KH;
            }
            else
            {
                return null;
            }
        }
    }
}